package com.afkt.project.assist;


import com.afkt.project.ui.widget.render.circle.jump.CircleBroodLoadingRenderer;
import com.afkt.project.ui.widget.render.scenery.LoadingViewDiv;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.HashMap;
import java.util.Map;

public class ViewAssist {
    private ViewAssist() {
    }


    // 标记 Tag
    private Object    mTag;
    // 包裹 View
    private ComponentContainer mWrapper;

    private       Object                mData;
    private final Map<String, Object> mMapDatas    = new HashMap<>();
    private final Map<Integer, Adapter> mMapAdapters = new HashMap<>();
    private final HashMap<Integer, Component> mTypeViews   = new HashMap<>(3);
    private       int                   mCurrentType = -1;
    private       Component                  mCurrentView;
    private       Listener              mListener;

    private Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    /**
     * 传入包裹 View
     * @param wrapper 包裹 View
     * @return {@link ViewAssist}
     */
    public static ViewAssist wrap(ComponentContainer wrapper) {
        return wrap(wrapper, null);
    }

    /**
     * 传入包裹 View
     * @param wrapper 包裹 View
     * @param tag     Tag
     * @return {@link ViewAssist}
     */
    public static ViewAssist wrap(
            ComponentContainer wrapper,
            Object tag
    ) {
        if (wrapper == null) return null;
        ViewAssist assist = new ViewAssist();
        assist.mWrapper = wrapper;
        assist.mTag = tag;
        return assist;
    }

    /**
     * 注册 type
     * @param type    Type
     * @param adapter {@link Adapter}
     * @return {@link ViewAssist}
     */
    public ViewAssist register(
            int type,
            Adapter adapter
    ) {
        if (adapter == null) return this;
        mMapAdapters.put(type, adapter);
        return this;
    }


    public interface Adapter {

        Component onCreateView(
                ViewAssist assist,
                LayoutScatter inflater
        );

        void onBindView(
                ViewAssist assist,
                Component view,
                int type
        );

    }


    public interface Listener {

        void onRemove(
                ViewAssist assist,
                int type,
                boolean removeView
        );

        void onNotFound(
                ViewAssist assist,
                int type
        );

        void onChange(
                ViewAssist assist,
                int type,
                int oldType
        );
    }


    // Type: 操作中、失败、成功、空数据
    public static final int TYPE_ING        = 1;
    public static final int TYPE_FAILED     = 2;
    public static final int TYPE_SUCCESS    = 3;
    public static final int TYPE_EMPTY_DATA = 4;

    public Object getTag() {
        return mTag;
    }

    public ViewAssist setTag(Object tag) {
        this.mTag = tag;
        return this;
    }

    /**
     * 显示 Type Adapter View
     * @param type Type
     */
    public void showType(int type) {
        if (mWrapper == null) return;
        Adapter adapter = mMapAdapters.get(type);
        if (adapter != null) {
            Component view = getView(type);
            if (view != null) {
                if (view instanceof LoadingViewDiv) {
                    view = new LoadingViewDiv(context);
                }
                if (view instanceof CircleBroodLoadingRenderer) {
                    view = new CircleBroodLoadingRenderer(context);
                }
                visible();
                int oldType = mCurrentType;
                mCurrentType = type;
                mCurrentView = view;
                mTypeViews.put(type, view);

                mWrapper.removeAllComponents();
                mWrapper.addComponent(view);


                adapter.onBindView(this, view, type);

                if (mListener != null) {
                    mListener.onChange(this, type, oldType);
                }
                return;
            }
        }
        //gone();
        mCurrentType = type;
        mCurrentView = null;
        mWrapper.removeAllComponents();
        if (mListener != null) {
            mListener.onNotFound(this, type);
        }
    }

    public Component getView(int type) {
        Component view = mTypeViews.get(type);
        if (view != null) return view;
        Adapter adapter = mMapAdapters.get(type);
        if (mWrapper == null || adapter == null) return null;
        view = adapter.onCreateView(this, LayoutScatter.getInstance(mWrapper.getContext()));
        mTypeViews.put(type, view);
        return view;
    }

    public ViewAssist visible() {
        if (mWrapper != null) mWrapper.setVisibility(Component.VISIBLE);
        return this;
    }

    public void showIng() {
        showType(TYPE_ING);
    }

    public void showFailed() {
        showType(TYPE_FAILED);
    }

    public void showSuccess() {
        showType(TYPE_SUCCESS);
    }

    public void showEmptyData() {
        showType(TYPE_EMPTY_DATA);
    }

}


